package org.jeecg.modules.sxaq.receiver;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.modules.sxaq.anr.entity.RecordAbnormal;
import org.jeecg.modules.sxaq.anr.service.IRecordAbnormalService;
import org.jeecg.modules.sxaq.deviceinfo.entity.DeviceInfo;
import org.jeecg.modules.sxaq.deviceinfo.service.IDeviceInfoService;
import org.jeecg.modules.sxaq.utils.RabbitMQConstant;
import org.jeecg.modules.sxaq.utils.SmsUtils;
import org.jeecg.modules.sxaq.utils.jpush.JiGuangPushService;
import org.jeecg.modules.sxaq.utils.jpush.JpushUtils;
import org.jeecg.modules.sxaq.utils.jpush.PushBean;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDeviceService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: MDM处理
 * @Author: DHC
 * @Date:  2020-12-01
 */
@Slf4j
@RabbitComponent(value = "localMDMListener")
public class MDMRabbitMqListener {

    @Autowired
    private IRecordAbnormalService recordAbnormalService;

    @Autowired
    private ISysUserDeviceService sysUserDeviceService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IDeviceInfoService deviceInfoService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private JpushUtils jpushUtils;
    @Autowired
    private SmsUtils smsUtils;

    @RabbitListener(concurrency = "2-10", bindings = {@QueueBinding(value = @Queue(value = RabbitMQConstant.LOCAL_MDM_QUEUE + "${sxaq.factoryCode}", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = RabbitMQConstant.LOCAL_MDM_EXCHANGE, type = "topic"),
            key = RabbitMQConstant.LOCAL_MDM_KEY + "${sxaq.factoryCode}")})
    public void onMessage(String message) {
        log.info("-------收到来自云端的mdm操作-------");
        try {
            JSONObject object = JSONObject.parseObject(message);

            String type = object.getString("type");
            String workNo = object.getString("workNo");

            if ("mdm".equals(type)) {
                SysUser user = sysUserService.getUserByWorkNo(workNo);
                if (user != null) {
                    String mobileSingle = object.getString("deviceId");
                    DeviceInfo deviceInfo = deviceInfoService.getDeviceBySingle(mobileSingle);

                    RecordAbnormal recordAbnormal = new RecordAbnormal();
                    recordAbnormal.setType(8);
                    recordAbnormal.setPhone(user.getPhone());
                    recordAbnormal.setRealname(user.getRealname());
                    recordAbnormal.setSysOrgCode(user.getOrgCode());
                    recordAbnormal.setUsername(user.getUsername());
                    recordAbnormal.setUserId(user.getId());
                    recordAbnormal.setEventTime(new Date(System.currentTimeMillis()));
                    recordAbnormal.setAnrId(UUID.randomUUID() + "_" + workNo);
                    recordAbnormal.setWorkNo(workNo);
                    recordAbnormal.setCreateTime(new java.util.Date(System.currentTimeMillis()));
                    recordAbnormal.setMobileModel(deviceInfo.getMobileModel());
                    recordAbnormal.setMobileSystem(deviceInfo.getMobileSystem());
                    recordAbnormal.setMobileVersion(deviceInfo.getMobileVersion());
                    recordAbnormal.setMobileSingle(deviceInfo.getMobileSingle());
                    recordAbnormalService.saveOrUpdate(recordAbnormal);

                    String depart = sysDepartService.queryDepartNameTxtByOrgCode(user.getOrgCode());

                    String push_app = depart + "的" + user.getRealname() + "[" + workNo + "]" + "在进入保密区域后，发生" + "卸载程序(APP)" + "事件。请相关部门及时处理！";
                    jpushUtils.push("卸载核心文件(APP)", push_app, null);

                    String push_sms = depart + "的" + user.getRealname() + "[" + workNo + "]";
                    smsUtils.sendSms(push_sms, "卸载核心文件(APP)");
                }
            }
        } catch (Exception e) {
            log.info("-------收到来自云端的mdm操作-------" + e);
        }
    }
}